package com.loan.controller.app.user;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.loan.common.annotation.Log;
import com.loan.common.core.controller.BaseController;
import com.loan.common.core.domain.PageQuery;
import com.loan.common.core.domain.Result;
import com.loan.common.core.page.TableDataInfo;
import com.loan.common.enums.BusinessType;
import com.loan.system.domain.UserBankEntity;
import com.loan.system.domain.UserEntity;
import com.loan.system.domain.UserRelationEntity;
import com.loan.system.domain.UserVaEntity;
import com.loan.system.service.OperatorLogService;
import com.loan.system.service.UserBankService;
import com.loan.system.service.UserRelationService;
import com.loan.system.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author shiguang on 2022/9/13 11:14
 */
@Validated
@Api(value = "用户管理", tags = {"用户管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/app/user")
public class AppUserController extends BaseController {

    private final UserService userService;

    private final UserBankService userBankService;

    private final UserRelationService userRelationService;

    private final OperatorLogService operatorLogService;


    /**
     * 获取用户列表
     */
    @ApiOperation("获取用户列表")
    @GetMapping("/page")
    public TableDataInfo<UserEntity> list(UserEntity entity, PageQuery pageQuery) {
        return userService.selectPageUserList(entity, pageQuery);
    }


    /**
     * 获取用户详情
     */
    @ApiOperation("获取用户详情")
    @GetMapping("/getUserInfo")
    public Result<UserEntity> getUserInfo(@RequestParam Long userId) {
        return Result.ok(userService.selectUserById(userId));
    }

    /**
     * 获取用户银行卡
     */
    @ApiOperation("获取用户银行卡")
    @GetMapping("/getUserBank")
    public Result<UserBankEntity> getUserBank(@RequestParam Long userId) {
        return Result.ok(userBankService.getUserBank(userId));
    }

    /**
     * 获取紧急联系人
     */
    @ApiOperation("获取紧急联系人")
    @GetMapping("/getUserRelation")
    public Result<List<UserRelationEntity>> getUserRelation(@RequestParam Long userId) {
        return Result.ok(userRelationService.getUserRelation(userId));
    }


    /**
     * 获取运营商记录
     */
    @ApiOperation("获取运营商记录")
    @GetMapping("/getUserOperatorLog")
    public Result<Object> getUserOperatorLog(@RequestParam Long userId) {
        return Result.ok(operatorLogService.getUserOperatorLog(userId));
    }

    @ApiOperation("更新用户银行卡")
    @Log(title = "修改用户银行卡", businessType = BusinessType.UPDATE)
    @PutMapping("/updateUserBank")
    public Result<Void> updateUserBank(@RequestParam Long id, @RequestParam Long bankId, @RequestParam String bankNo, @RequestParam Long userId) {
        return toAjax(userBankService.updateUserBank(id, bankId, bankNo, userId));
    }


    @ApiOperation("根据订单id获取用户虚拟卡")
    @GetMapping("/getUserVa")
    public Result<UserVaEntity> getUserVa(@RequestParam Long applyId) {
        return Result.ok(userService.getUserVaByApplyId(applyId));
    }

    /**
     * 删除用户
     */
    @ApiOperation("删除用户")
    @Log(title = "删除用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userId}")
    public Result<Void> remove(@ApiParam("ID") @PathVariable("userId") Long userId) {
        userService.deleteUserById(userId);
        return Result.ok();
    }
}


